home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / cimr.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  5KB  |  176 lines

  1. /* Citadel Internet Mail reader V1.00
  2. ** this routine will scan a directory specified in arg1
  3. ** Each entry is expected to be a mail message.
  4. ** The subject is tested against arg2(if found)
  5. **
  6. */
  7. #include <libraries/dos.h>
  8. #include <dos.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <proto/dos.h>
  13. #include "sysdep.h"
  14.  
  15. short level;
  16. extern int _OSERR;
  17.  
  18. int Process_File(char *mail, char *subject, char *dump, char *processed);
  19. int main(int,char **);
  20. int Do_Break(void);
  21. char *Find_Text(FILE *fp, char *text);
  22.  
  23. int main( argc, argv)
  24. int argc;     /* should be 4 */
  25. char *argv[]; /* current location */
  26.   {
  27.   int status;
  28.   printf("Citadel Internet Mail Reader %s\n",VERSION_NAME);
  29.  
  30.   if( onbreak(&Do_Break) )printf("Cannot set break for ^C");
  31.  
  32.   if( argc != 5 )
  33.     {
  34.     printf("Please setup this program with the proper arguments.\n");
  35.     printf("CIMR arg1 arg2 arg3 arg4\n");
  36.     printf("   arg1: This is the mail full path name such as cit:mail/*.am\n");
  37.     printf("         which is where your mailer stores mail messages\n");
  38.     printf("   arg2: This is the subject header for the messages to file\n");
  39.     printf("         to search for.\n");
  40.     printf("   arg3: Where to dump the uuencoded message(full pathname)\n");
  41.     printf("   arg4: What do we do after processing\n");
  42.     printf(" This program will read the files specified in arg1, find the subject\n");
  43.     printf(" header specified in arg2 dumping the uuencoded portion(begin to end)\n");
  44.     printf(" into arg3, then save the used message (arg4=save) or remove it(arg4=remove\n");
  45.     printf("\n");
  46.     printf(" Status returned is 0 if message found\n");
  47.     printf("                    5 if message not found\n");
  48.     printf("                   20 if error occured\n");
  49.     status = 20;
  50.     }
  51.   else  status = Process_File(argv[1], argv[2], argv[3], argv[4]);
  52.   return status;
  53.   }
  54.  
  55. int Process_File(char *mail, char *subject, char *dump, char *processed)
  56.   {
  57.   FILE *fp;
  58.   FILE *op;
  59.   struct FileInfoBlock *info;
  60.   int error,attr;
  61.   char *ptr, *tptr;
  62.   char *dir;
  63.   char fullname[128];
  64.   dir = strdup(mail);
  65.   ptr = &dir[strlen(dir)-1];
  66.   while( *ptr != '/' && *ptr != ':' && ptr != dir)ptr--;
  67.   ptr[1] = '\0';
  68.   info  = (struct FileInfoBlock *)calloc(1,sizeof(struct FileInfoBlock));
  69.   if( info == NULL )
  70.     {
  71.     printf("Unable to get memory for FileInfoBlock, aborting\n");
  72.     return 20;
  73.     };
  74.  
  75.   attr = 1; /* find all files not directories */
  76.   error = dfind(info,mail,attr);  /* get first one*/
  77.   while( error == 0 )
  78.     {
  79.     /**
  80.       For each file, read the lines until a subject is found and
  81.       process the subject.
  82.     **/
  83.     printf("processing %s\n",info->fib_FileName);
  84.     strcpy(fullname,dir);
  85.     strcat(fullname,info->fib_FileName);
  86.     if(  (fp = fopen(fullname,"r")) == NULL )
  87.       {
  88.       printf("unable to open, skipping it...\n");
  89.       }
  90.     else
  91.       {
  92.       if( ( ptr = Find_Text(fp,"subject")) != NULL )
  93.         {
  94.         tptr = &ptr[9];            /* start of subject */
  95.         if( strnicmp(tptr,subject,strlen(subject)) == 0)
  96.           {
  97.           free(ptr);
  98.           if( ( ptr = Find_Text(fp,"begin 644") ) != NULL )
  99.             {
  100.             if( ( op=fopen(dump,"w")) == NULL )
  101.               {
  102.               printf("unable to open output file:%s\n",dump);
  103.               free(ptr);
  104.               return 20;
  105.               }
  106.             else
  107.               {
  108.               char line[80];
  109.               /* copy the file to the output */
  110.               fputs(ptr,op);
  111.               free(ptr);
  112.               while( fgets(line,sizeof(line),fp) )fputs(line,op);
  113.               fclose(op);
  114.               fclose(fp);
  115.               /*
  116.                check processed for "save" or "remove"
  117.               */
  118.               if( stricmp(processed,"remove") == 0 )
  119.                 {
  120.                 printf("deleting %s\n",fullname);
  121.                 remove(fullname);
  122.                 }
  123.               else if( stricmp(processed,"save") != 0 )
  124.                 {
  125.                 printf(" Invalid option, should be save or remove was %s\n",processed);
  126.                 printf(" assumed save\n");
  127.                 };
  128.               return 0;   /* success! */
  129.               };
  130.             };
  131.           }
  132.         else
  133.           {
  134.           free(ptr);
  135.           fclose(fp);
  136.           };
  137.         }
  138.       else
  139.         {
  140.         free(ptr);
  141.         };
  142.       fclose(fp);
  143.       };
  144.     error = dnext(info);
  145.     };
  146.  
  147.   return 5;
  148.   }
  149.  
  150. char *Find_Text(FILE *fp, char *text)
  151.   {
  152.   char line[80];  /* assume lines are 80 char or less */
  153.   int  len;
  154.   /**
  155.     read the file processing each line until we find "text"
  156.     as the first thing on the line.
  157.   **/
  158.   len = strlen(text);
  159.   while( fgets(line,sizeof(line),fp) )
  160.     {
  161.     if( strnicmp(text,line,len) == 0 )
  162.       {
  163.       return strdup(line);
  164.       };
  165.     };
  166.   return NULL;
  167.   }
  168.  
  169. int Do_Break()
  170.   {
  171.   printf("\n ^C - ***Break***\n ");
  172.   (void)fcloseall();
  173.   return(1);
  174.  
  175.   }
  176.